import { UPDATE_CART, UPDATE_SKU, UPDATE_CART_NUMS, DELETE_PRODUCT, CLEAR_CART } from '../mutations-type'
import { pay } from '../../services/cart'

export default {
  namespaced: true,
  state: {
    products: []
  },
  getters: {
    total (state: { products: { reduce: (arg0: (total: any, product: any) => any, arg1: number) => void; }; }, getters: any) {
      return state.products.reduce((total, product) => {
        return total + product.price * product.nums
      }, 0)
    }
  },
  mutations: {
    [UPDATE_CART] (state: { products: any[]; }, product: { id: any; sku: any; }) {
      const index = state.products.findIndex(item => item.id === product.id)
      if (index < 0) {
        state.products.push({
          ...product,
          nums: 1,
          max: product.sku
        })
      } else {
        state.products[index].nums++
      }
    },
    [UPDATE_CART_NUMS] (state: { products: { find: (arg0: (item: any) => boolean) => { nums: any; }; }; }, { id, nums }: any) {
      state.products.find(item => item.id === id).nums = nums
    },
    [DELETE_PRODUCT] (state: { products: { filter: (arg0: (item: any) => boolean) => void; }; }, product: { id: any; }) {
      // @ts-ignore
      state.products = state.products.filter(item => item.id !== product.id)
    },
    [CLEAR_CART] (state: { products: never[]; }, product: any) {
      state.products = []
    }
  },
  actions: {
    addToCart ({ commit }: any, product: { id: any; sku: number; }) {
      commit(UPDATE_CART, product)

      commit('products/' + UPDATE_SKU, {
        id: product.id,
        sku: product.sku - 1
      }, { root: true })
    },
    // @ts-ignore
    onChangeNums ({ commit }, { product, val }) {
      if (val >= product.max) {
        commit(UPDATE_CART_NUMS, { id: product.id, nums: product.max })

        commit('products/' + UPDATE_SKU, {
          id: product.id,
          sku: 0
        }, { root: true })
      } else {
        commit(UPDATE_CART_NUMS, { id: product.id, nums: val })

        commit('products/' + UPDATE_SKU, {
          id: product.id,
          sku: product.max - val
        }, { root: true })
      }
    },
    // @ts-ignore
    deleteProduct ({ commit }, product) {
      commit(DELETE_PRODUCT, product)

      commit('products/' + UPDATE_SKU, {
        id: product.id,
        sku: product.max
      }, { root: true })
    },
    // @ts-ignore
    async pay ({ commit, state }, payload) {
      const ids = state.products.map((item: { id: any; }) => item.id)
      // @ts-ignore
      const { data } = await pay(ids)

      if (data) {
        alert('结算成功')
        // @ts-ignore
        state.products.forEach(product => {
          commit('products/' + UPDATE_SKU, {
            id: product.id,
            sku: product.max
          }, { root: true })
        })

        commit(CLEAR_CART)
      } else {
        alert('结算失败')
      }
      return Promise.resolve(data)
    }
  }
}
